<template>
	<view>
		<view class="mb-2">
			<view>开奖号码：</view>
			<uni-easyinput v-model="drawResult" placeholder="开奖号码" class="my-2"></uni-easyinput>
			<uni-data-select v-model="activeIssue" :localdata="historyData" @change="onChangeIssue"
				placeholder="最近 10 期快捷选择"></uni-data-select>
		</view>
		<view v-show="!importPanelInfo.visible">
			<view>
				<view class="flex items-center my-2" v-for="(item, idx) in data" :key="idx">
					<view>#{{ idx + 1 }}</view>
					<view class="mx-4" v-html="item.text"></view>
					<view :class="item.money > 0 ? 'text-red-400' : ''">
						{{ item.money > 0 ? (item.frontCount + ' + ' + item.backCount) : '-' }}
					</view>
					<view class="text-error ms-4">{{item.statusText + (item.status>0 ? `（${item.moneyText}元）` : '')}}
					</view>
				</view>
			</view>
		</view>
		<view class="my-2" v-show="importPanelInfo.visible">
			<view>批量导入号码：</view>
			<uni-easyinput type="textarea" v-model="importPanelInfo.content" placeholder="请输入号码，每注号码一行"
				:rows="10"></uni-easyinput>
		</view>

		<view class="mt-2" v-show="!importPanelInfo.visible">
			<button size="mini" type="primary" @click="importPanelInfo.visible = true">编辑号码</button>
			<button class="mx-1" size="mini" type="info" @click="onCopy()">复制</button>
		</view>
		<view class="mt-2" v-show="importPanelInfo.visible">
			<button size="mini" type="primary" @click="onCalc()">立即计算</button>
			<button class="mx-1" size="mini" @click="importPanelInfo.content = ''">清空</button>
			<button size="mini" @click="onReadTemp()">读取暂存内容</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import DltOrderModel from '@/model/DltOrderModel';
	import DltDataModel from '@/model/DltDataModel';
	import FsDriver from '@/model/FsDriver';
	import {
		copyText
	} from '@/utils/Utils';

	const fsDriver = new FsDriver();
	const dltDataModel = new DltDataModel();
	const dltOrderModel = new DltOrderModel();
	const dataset = dltDataModel.getDataSource().slice(0, 10);
	const historyData = dataset.map((v, i) => {
		return {
			text: v.lotteryDrawNum,
			value: i
		};
	});
	const drawResult = ref('');
	const activeIssue = ref('');
	const importPanelInfo = ref({
		visible: true,
		content: ''
	})
	const rowTpl = {
		order: '',
		text: '',
		frontCount: 0,
		backCount: 0,
		totalCount: 0,
		statusText: '',
		status: '',
		moneyText: '',
		money: 0,
		sum: 0,
		diffSum: 0,
	};
	const data = ref([rowTpl])

	const onChangeIssue = (idx) => {
		drawResult.value = dataset[idx].lotteryDrawResult;
	}

	const onCalc = () => {
		if (drawResult.value.trim().length == 0) {
			uni.showToast({
				title: '开奖号码无效',
				icon: 'error'
			})
			return;
		}
		if (importPanelInfo.value.content.trim().length == 0) {
			uni.showToast({
				title: '投注号码无效',
				icon: 'error'
			})
			return;
		}

		data.value = [];
		importPanelInfo.value.visible = false;
		let orderList = data.value.filter(v => v.order.length > 0).map(v => v.order);
		importPanelInfo.value.content.trim().split('\n').forEach(v => {
			v = v.replace(/\,|"/g, ' ').trim();
			if (v.length > 0 && !orderList.includes(v)) {
				orderList.push(v);
				data.value.push({
					...rowTpl,
					order: v,
					text: v
				})
			}
		})
		if (data.value[0].order == '') {
			data.value = data.value.slice(1);
		}
		data.value = dltOrderModel.calcOrder(drawResult.value, orderList);
	}

	const onCopy = () => {
		let arr = []
		data.value.forEach(item => {
			if (item.money > 0) {
				arr.push(
					`${item.order} ${item.statusText}【${item.moneyText}元】【${item.frontCount}+${item.backCount}】`
				)
			} else {
				arr.push(`${item.order} ${item.statusText}`)
			}
		})
		copyText(arr.join('\n'));
		uni.showToast({
			title: '复制成功',
			icon: 'success'
		})
	}

	const onReadTemp = () => {
		let dataStr = fsDriver.get('temp-order');
		if (!dataStr) {
			uni.showToast({
				title: '没有暂存的数据',
				icon: 'error'
			})
			return;
		}
		let data = JSON.parse(dataStr);
		importPanelInfo.value.content = data.join('\n');
	}
</script>